package matlap.client.view.admin;

import java.util.Iterator;
import java.util.List;

import matlap.client.services.news.NewsService;
import matlap.client.services.news.NewsServiceAsync;
import matlap.shared.New;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.TextArea;

public class AdminNewsComposite extends Composite {
	Panel vp = new VerticalPanel();
	Panel newPanel = new HorizontalPanel();
	Panel updatedeletePanel = new VerticalPanel();
	Panel buttonPanel = new HorizontalPanel();
	Panel simplePanel = new VerticalPanel();
	NewsServiceAsync ns = GWT.create(NewsService.class);
	ListBox titleBox;
	Button newButton;
	Button updateButton;

	public AdminNewsComposite() {
		initWidget(vp);
		initButtonPanel();
		vp.add(buttonPanel);
		initNewPanel();
		initUpdateDeletePanel();
		simplePanel.add(newPanel);
		vp.add(simplePanel);
		updateButtonColor(1, 0);

	}

	public void updateButtonColor(int c1, int c2) {
		if (c1 == 0) {
			newButton.setStyleName("Button1");
		} else {
			newButton.setStyleName("Button2");
		}
		if (c2 == 0) {
			updateButton.setStyleName("Button1");
		} else {
			updateButton.setStyleName("Button2");
		}

	}

	public void initButtonPanel() {
		newButton = new Button("H\u00EDr felvezet\u00E9se");
		newButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				updateButtonColor(1, 0);
				simplePanel.clear();
				simplePanel.add(newPanel);
			}

		});
		buttonPanel.add(newButton);
		newButton.setWidth("341px");
		updateButton = new Button("H\u00EDrek szerkeszt\u00E9se");
		updateButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				updateButtonColor(0, 1);
				simplePanel.clear();
				simplePanel.add(updatedeletePanel);
			}
		});
		buttonPanel.add(updateButton);
		updateButton.setWidth("341px");
	}

	public void initNewPanel() {
		FlexTable fl = new FlexTable();
		newPanel.add(fl);
		fl.setSize("667px", "240px");

		Label vnLabel = new Label("H\u00EDr c\u00EDme");
		fl.setWidget(1, 0, vnLabel);
		vnLabel.setWidth("115px");

		final TextBox textBox = new TextBox();
		fl.setWidget(1, 1, textBox);

		Label lblNewLabel_1 = new Label("H\u00EDr tartalma");
		fl.setWidget(2, 0, lblNewLabel_1);

		final TextArea textArea = new TextArea();
		fl.setWidget(2, 1, textArea);
		textArea.setSize("390px", "133px");

		Button btnNewButton = new Button("New button");
		btnNewButton.setText("Ment\u00E9s");
		fl.setWidget(3, 1, btnNewButton);
		btnNewButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				String title = textBox.getText();
				String content = textArea.getText();

				if ((title.length() == 0) || (content.length() == 0)) {
					Window.alert("nem adott meg mindent!");
				} else {
					textBox.setText("");
					textArea.setText("");
					New n = new New(title, content);
					ns.insertNews(n, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("hiba");

						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Sikeres");
							initTitleBox();

						}

					});
				}

			}

		});

	}

	public void initUpdateDeletePanel() {
		FlexTable fl2 = new FlexTable();
		updatedeletePanel.add(fl2);
		fl2.setSize("667px", "240px");

		Label vnLabel2 = new Label("H\u00EDr c\u00EDme");
		fl2.setWidget(1, 0, vnLabel2);
		vnLabel2.setWidth("115px");

		titleBox = new ListBox();
		fl2.setWidget(1, 1, titleBox);
		initTitleBox();

		Label lblNewLabel_1 = new Label("H\u00EDr tartalma");
		fl2.setWidget(2, 0, lblNewLabel_1);

		final TextArea textArea2 = new TextArea();
		fl2.setWidget(2, 1, textArea2);
		textArea2.setSize("390px", "133px");

		Button btnNewButton_1 = new Button("New button");
		btnNewButton_1.setText("M\u00F3dos\u00EDt\u00E1s");
		fl2.setWidget(3, 0, btnNewButton_1);
		btnNewButton_1.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				String title = titleBox.getItemText(titleBox.getSelectedIndex());
				String content = textArea2.getText();

				if ((title.length() == 0) || (content.length() == 0)) {
					Window.alert("nem adott meg mindent!");
				} else {
					titleBox.setSelectedIndex(0);
					textArea2.setText("");
					New n = new New(title, content);
					ns.updateNews(n, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("hiba");

						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Sikeres");
							initTitleBox();

						}

					});
				}

			}

		});

		Button btnNewButton2 = new Button("New button");
		btnNewButton2.setText("T\u00F6rl\u00E9s");
		btnNewButton2.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent sender) {
				String title = titleBox.getItemText(titleBox.getSelectedIndex());
				String content = textArea2.getText();

				if ((title.length() == 0) || (content.length() == 0)) {
					Window.alert("nem adott meg mindent!");
				} else {
					titleBox.setSelectedIndex(0);
					textArea2.setText("");
					New n = new New(title, content);
					ns.deleteNews(n, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("hiba");

						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Sikeres");
							initTitleBox();

						}

					});
				}

			}

		});

		fl2.setWidget(3, 1, btnNewButton2);
		fl2.getCellFormatter().setHorizontalAlignment(3, 0,
				HasHorizontalAlignment.ALIGN_RIGHT);
		fl2.getCellFormatter().setHorizontalAlignment(3, 1,
				HasHorizontalAlignment.ALIGN_CENTER);
		titleBox.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent sender) {
				String title = titleBox.getItemText(titleBox.getSelectedIndex());
				if (title.length() > 0) {
					ns.getNewsByTitle(title, new AsyncCallback<New>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("hiba");

						}

						@Override
						public void onSuccess(New result) {
							textArea2.setText(result.getContent());

						}

					});
				}

			}

		});
	}

	public void initTitleBox() {
		titleBox.clear();
		titleBox.addItem("");
		ns.getNews(new AsyncCallback<List<New>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(List<New> result) {
				Iterator<New> iterator = result.iterator();
				while (iterator.hasNext()) {
					New n = (New) iterator.next();
					titleBox.addItem(n.getTitle());
				}
			}

		});

	}

}
